<?php

/**
 * Gets value of configuration parameter
 * @param string $k parameter name
 * @return mixed parameter value
 */
function _cfg($k, $key = null, $clear = false)
{
	static $cfg = array();
	if ($clear)
		unset($cfg[$k][$clear]);
	if (!$cfg)
	{
		// get common settings
		include 'config/server.php';
		include 'config/common.php';
		// get modules settings
		$mods = glob($cfg['path'] . '_inc/mod/*', GLOB_ONLYDIR);
		foreach ($mods as $mod)
		{
			include $mod.'/config/config.php';
		}
		//gets settings from db, overrides file settings if names are equal
		$db = Db::get($cfg['db']);
		$sql = 'SELECT code, val FROM '._t('settings');
		$cfg = array_merge($cfg, $db->getHash($sql));
	}
	return $key == null ? $cfg[$k] : $cfg[$k][$key];
}

/**
 * Gets table name by alias. Checks for table renames
 *
 * @param string $name table name
 * @return string
 */
function _t($name)
{
	static $tbl = array();
	if (!$tbl)
	{
		include 'config/tables.php';
	}
	return (empty($tbl[$name]) ? $name : $tbl[$name]);
}

/** Returns formatted mesage
 * @param  string $k message code
 * @param  mixed  $param array or string of message parameters
 * @return string formateed message
 */
function _fmt($k, $param=array())
{

	static $msg = array();
	if (!$msg){
		// get common settings
		include 'config/messages.php';
		// get modules messages
		$mods = glob(_cfg('path') . '_inc/mod/*', GLOB_ONLYDIR);
		foreach ($mods as $mod)
			if (file_exists($mod.'/config/messages.php'))
			include $mod.'/config/messages.php';
	}
	if ($param && !is_array($param))
		$param = array($param);
	if(array_key_exists($k.".".$_SESSION['lang'],$msg)){
		return vsprintf($msg[$k.".".$_SESSION['lang']], $param);
	}
	return vsprintf($msg[$k], $param);
}

/** Formats date
*/
function _dformat($d, $format='')
{
	if (!$format)
		$format = _cfg('format.date');
	if (!is_numeric($d))
		$d = strtotime($d);
	return date($format, $d);
}

/**
 * Prints human readable info about $param
 *
 * @param mixed $param info to display
 * @param bool $isDie
 */
function _d($param, $isDie=false)
{
	echo '<hr><pre>';
	print_r($param);
	echo '</pre><hr>';
	if ($isDie)
		die();
}

/** Sets custom error handler
*/
function _ehandler($errNo, $errMsg, $fileName, $lineNum, $vars)
{
	if ((_cfg('debug.level') & $errNo) == 0)
		return;

	$types = array (
		2   =>  "Warning",
		8   =>  "Notice",
		256 =>  "User Error",
		512 =>  "User Warning",
		1024=>  "User Notice",
	);

	$info = array(
		'errNo'  => $types[$errNo],
		'errMsg' => $errMsg,
		'files'  => array(),
	);

	$files = debug_backtrace();
	for ($i=1, $n=sizeof($files); $i<$n; ++$i)
		$info['files'][] = $files[$i]['line'].' , '.$files[$i]['file'];

	if (_cfg('debug'))
		_d($info, 1);
	else
	{
		if (empty($_SERVER['SERVER_NAME']))
			$_SERVER['SERVER_NAME'] = 'Project';

		ini_set ("SMTP", "mail.weborchestra.org");
		mail(_cfg('debug.mail'), 'Script Error at '.$_SERVER['SERVER_NAME'], print_r($info,1));
	}
}

// Functin save data into variable
function _storage($name, $value = FALSE)
{
	static $noteStorage = array();
	if ($value)
		$noteStorage[$name] = $value;
	return $noteStorage[$name];
}

// Set active menu item
function m($code)
{
	_storage('menu.current', $code);
}

// Get is active menu item
function is_m($code) {
	return _storage('menu.current') == $code;
}
